package com.example.fin;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.fin.bean.FinItem;

public class MainActivity extends AppCompatActivity {
    private String userName = "zhiwen";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView mListView = findViewById(R.id.lst_fin);
        mListView.setAdapter(new QuoteAdapter(this));

        findViewById(R.id.btn_add).setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, EditFrame.class);
            startActivity(intent);
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView arg0, View arg1, int
                    position, long arg3) {
//                Intent i = new Intent(QuoteReaderActivity.this,
//                        QuoteDetail.class);
//                i.putExtra("position", position);
//                startActivity(i);
            }
        });

    }


    class QuoteAdapter extends BaseAdapter {
        private Context mContext;
        private LayoutInflater mInflator;
        private DataSource mDataSource;

        public QuoteAdapter(Context c) {
            mContext = c;
            mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mDataSource = new DataSource(c);
            mDataSource.list(userName);
        }

        @Override
        public int getCount() {
            return mDataSource.getSize();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflator.inflate(R.layout.activity_list_item, parent, false);
            }
            ImageView imgMark = convertView.findViewById(R.id.imageView);
            TextView txtDate = convertView.findViewById(R.id.txtDate);
            TextView txtFee = convertView.findViewById(R.id.txtFee);
            TextView txtAmount = convertView.findViewById(R.id.txtAmount);
            TextView txtRemark = convertView.findViewById(R.id.txtRemark);
            FinItem item = mDataSource.getData(position);
            txtDate.setText(item.getDate());
            txtFee.setText(item.getFee());
            txtAmount.setText(item.getAmount());
            txtRemark.setText(item.getRemark());
//            imgMark.setImageResource(item.getDate());

            return convertView;
        }


    }
}